/*
 * @Author: junhuafly
 * @Date: 2020-02-27 18:52:53
 * @LastEditTime: 2020-04-07 09:51:15
 * @LastEditors: junhuafly
 * @Description: In User Settings Edit
 * @FilePath: \weChat\components\sku\index.js
 */
// components/sku/index.js
import {
  getSkuInfo,
} from './../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    initSku:{
      type:Object,
      default:{}
    },
    goodsId:Number,
  },
  observers: {
    'goodsId':function(val){
      if(!val) return
      getSkuInfo.call(this, val, this.data.initSku||{})
      const {sizeVal,colorVal,count,max} = this.data.initSku||{}
      if(sizeVal&&colorVal&&count){
        this.setData({
          colorVal,
          sizeVal,
          count,
          max
        })
      }else{
        this.setData({
          colorVal:'',
          sizeVal:'',
          count:1,
          max:-1
        })
      }
    }
  },

  ready(){
    // 若颜色或尺码只存在一个的时候，默认选中
   
  },
  /**
   * 组件的初始数据
   */
  data: {
    colorVal:'',
    sizeVal:'',
    count:1,
    max:3,
    curSKU:{},
    saleInfo:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    closeSku(){
      this.triggerEvent('close')
    },
    querySku(){
      this.triggerEvent('get_sku',{quantity:this.data.count,storageId:this.data.curSKU.storageId})
    },
    setSku(){
      this.setData({
        curSKU: this.data.saleInfo.saleSKU[`${this.data.colorVal}_${this.data.sizeVal}`]||{}
      })
     
    },
    changeCount({detail}){
      this.setData({
        count:detail
      })
    },
    onChange({target:{dataset:{type}},detail}){
      const obj = {
        color:'colorVal',
        size:'sizeVal'
      }
      this.setData({
        [obj[type]]:this.data[`${type}Val`]===detail?'':detail        
      })
      this.setSku()
      const sku = this.data.curSKU
      console.log('sku', sku)
      this.setData({
        'saleInfo.total_stock':sku.storageQuantity,
        max: sku.storageQuantity||1,
        disBuy: sku.storageQuantity===0,
        'saleInfo.price': sku.price
      })
    }
  }
})
